package com.project.housesalesystem.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.housesalesystem.bean.House;
import com.project.housesalesystem.common.PageResult;
import com.project.housesalesystem.common.Results;
import com.project.housesalesystem.service.HouseService;
<<<<<<< HEAD
import jakarta.servlet.http.HttpServletRequest;
=======
>>>>>>> 66dcee4e153c16ef05fd0c7e1ed9f414a0bf0bc9
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

<<<<<<< HEAD
=======
import javax.servlet.http.HttpServletRequest;
>>>>>>> 66dcee4e153c16ef05fd0c7e1ed9f414a0bf0bc9
import java.util.*;

@RequestMapping("admin")
@RestController
public class AdminHouseController {
    private static final Logger logger = LoggerFactory.getLogger(AdminHouseController.class);

    @Autowired
    private HouseService houseService;
    /**
     * 后端列表
     */
    @RequestMapping("/page")
    public PageResult<House> page(@RequestParam Map<String, Object> params, HttpServletRequest request){
        logger.debug("page方法:,,Controller:{},,params:{}",this.getClass().getName(), JSONObject.toJSONString(params));

        IPage<House> page = houseService.getHousePage(params);

        return PageResult.success(page);
    }



    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public Results info(@PathVariable("id") Long id, HttpServletRequest request){
        logger.debug("info方法:,,Controller:{},,id:{}",this.getClass().getName(),id);
        House house = houseService.getById(id);
        if(house !=null){
            return Results.ok().put("data", house);
        }else {
            return Results.error(511,"查不到数据");
        }

    }

    /**
     * 后端保存
     */
    @RequestMapping("/save")
    public Results save(@ModelAttribute  House house, HttpServletRequest request){
        logger.debug("save方法:,,Controller:{},,house:{}",this.getClass().getName(),house.toString());

        Wrapper<House> queryWrapper = new LambdaQueryWrapper<House>()
                .eq(House::getOrderNum, house.getOrderNum());

        logger.info("sql语句:"+queryWrapper.getSqlSegment());
        House houseEntity = houseService.getOne(queryWrapper);
        if(houseEntity==null){
            house.setOrderNum(UUID.randomUUID().toString());
            houseService.save(house);
            return Results.ok();
        }else {
            return Results.error(511,"表中有相同数据");
        }
    }

    /**
     * 后端修改
     */
    @RequestMapping("/update")
    public Results update(@ModelAttribute House house, HttpServletRequest request){
        logger.debug("update方法:,,Controller:{},,house:{}",this.getClass().getName(),house.toString());

        //根据字段查询是否有相同数据
        Wrapper<House> queryWrapper = new LambdaQueryWrapper<House>().eq(House::getName, house.getName());

        logger.info("sql语句:"+queryWrapper.getSqlSegment());
        House houseEntity = houseService.getOne(queryWrapper);
        if("".equals(house.getPic()) || "null".equals(house.getPic())){
            house.setPic("");
        }
        if(houseEntity==null){
            houseService.updateById(house);//根据id更新
            return Results.ok();
        }else {
            return Results.error(511,"表中有相同数据");
        }
    }

    /**
     * 上下架状态修改
     */
    @RequestMapping("/updateState")
    public Results updateState(@ModelAttribute House house, HttpServletRequest request){
        logger.debug("update方法:,,Controller:{},,house:{}",this.getClass().getName(),house.toString());
        houseService.updateHouseState(house.getState(),house.getId());//根据id更新
        return Results.ok();

    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Results delete(@ModelAttribute String ids){
        logger.debug("delete:,,Controller:{},,ids:{}",this.getClass().getName(),ids);
        houseService.removeByIds(Arrays.asList(ids.split(",")));
        return Results.ok();
    }

//

}

